package cn.edu.zut.hkxy.util;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

public class FileUploadUtils {
    public static String imgPath="imgPath";
    public static String docPath="docPath";
    public static String imgUrlPrefix="imgUrlPrefix";
    public static String docUrlPrefix="docUrlPrefix";
    public static String getPathOrUrl(String key){
        Properties properties=new Properties();
        try {
            File propertyFile=ResourceUtils.getFile("classpath:FileUpload.properties");
            properties.load(new FileInputStream(propertyFile));
            String value=(String)properties.get(key);
            return value;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String saveFile(MultipartFile file) throws IOException {
        String path=FileUploadUtils.getPathOrUrl(FileUploadUtils.docPath);
        String originalName = file.getOriginalFilename();
        String name=UUID.randomUUID().toString()+originalName.substring(originalName.lastIndexOf("."));
        File file1=new File(path,name);
        System.out.println(file1.getPath());
        String url=FileUploadUtils.getPathOrUrl(FileUploadUtils.docUrlPrefix)+name;
        file.transferTo(file1);
        return url;
    }
    public static Map<String,String> saveImgGetPath(MultipartFile file) throws IOException {
        String path=FileUploadUtils.getPathOrUrl(FileUploadUtils.imgPath);
        String originalName = file.getOriginalFilename();
        String name=UUID.randomUUID().toString()+originalName.substring(originalName.lastIndexOf("."));
        File file1=new File(path,name);
        System.out.println(file1.getPath());
        String url=FileUploadUtils.getPathOrUrl(FileUploadUtils.imgUrlPrefix)+name;
        file.transferTo(file1);
        Map<String,String> map=new HashMap<>();
        map.put("url",url);
        map.put("path",file1.getPath());
        return map;
    }

    public static Map<String,String> saveDocsGetPath(MultipartFile file)
                                                    throws IOException {
        String path=FileUploadUtils.getPathOrUrl(FileUploadUtils.docPath);
        String originalName = file.getOriginalFilename();
        String name=UUID.randomUUID().toString()
                    +originalName.substring(originalName.lastIndexOf("."));
        File file1=new File(path,name);
        System.out.println(file1.getPath());
        String url=FileUploadUtils.getPathOrUrl(FileUploadUtils.docUrlPrefix)+name;
        file.transferTo(file1);
        Map<String,String> map=new HashMap<>();
        map.put("url",url);
        map.put("path",file1.getPath());
        return map;
    }

    public static void deleteFile(String filePath){
        System.out.println(filePath);
        File file=new File(filePath);
        if (file.exists() && file.isFile()) {
            System.out.println("文件存在！");
            file.delete();
            System.out.println("已删除");
        }
    }

}
